/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.IssuesDAO;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.entities.Response;
import br.com.deployment.genericdao.GenericDAO;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class IssuesDAOImpl extends GenericDAO<Issues> implements IssuesDAO{

    public IssuesDAOImpl(){
        super(Issues.class);
    }
    
    
    @Override
    public List<Issues> findIssues(Issues issues) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Issues> findIssues(Category category) {
        String sql = "SELECT i FROM Category c JOIN c.issueses i WHERE"
                     + " c.id = :id ";
       Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", category.getId());
        return (List<Issues>) query.getResultList();
    }

    @Override
    public List<Issues> findIssuesType(Category category, Questionnaire q) {
        String sql = "SELECT i FROM Issues i JOIN i.category ci "
                + " JOIN  i.questionnaire q WHERE"
                     + " ci.id = :id and q.id = :idQ and i.active= :active ORDER BY i.id ASC ";
       Query query = getEntityManager().createQuery(sql);
       query.setParameter("id", category.getId());
       query.setParameter("idQ", q.getId()); 
        query.setParameter("active", true); 
      
        return (List<Issues>) query.getResultList();
    }

    @Override
    public List<Issues> findIssuesResponse(Response response) {
       String sql = "SELECT i FROM Response r JOIN r.issues i WHERE"
                     + " r.id = :id  and i.active= :active ";
       Query query = getEntityManager().createQuery(sql);
       query.setParameter("id", response.getId());
       query.setParameter("active", true); 
       return (List<Issues>) query.getResultList();
    }
    
    @Override
    public Issues findIssuesResponseOne(Response response) {
         String sql = "SELECT i FROM Response r JOIN r.issues i WHERE"
                     + " r.id = :id and i.active= :active";
       Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", response.getId());
        query.setParameter("active", true); 
     
        return (Issues) query.getSingleResult();
    }

   
    
}
